package org.jeecg.modules.peopleTask.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.peopleTask.entity.AlgemeenDagblad;
import org.jeecg.modules.peopleTask.entity.MessageLogging;
import org.jeecg.modules.peopleTask.service.IAlgemeenDagbladService;
import org.jeecg.modules.peopleTask.service.IMessageLoggingService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.weeklyTasks.entity.WeeklyTasks;
import org.jeecg.modules.weeklyTasks.service.IWeeklyTasksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Calendar;

/**
 * @Description: 每日汇报
 * @Author: jeecg-boot
 * @Date:   2023-12-20
 * @Version: V1.0
 */
@Api(tags="每日汇报")
@RestController
@RequestMapping("/peopleTask/algemeenDagblad")
@Slf4j
public class AlgemeenDagbladController extends JeecgController<AlgemeenDagblad, IAlgemeenDagbladService> {
	@Autowired
	private IAlgemeenDagbladService algemeenDagbladService;
	@Autowired
	private IMessageLoggingService messageLoggingService;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private IWeeklyTasksService weeklyTasksService;
	
	/**
	 * 分页列表查询
	 *
	 * @param algemeenDagblad
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "每日汇报-分页列表查询")
	@ApiOperation(value="每日汇报-分页列表查询", notes="每日汇报-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(AlgemeenDagblad algemeenDagblad,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AlgemeenDagblad> queryWrapper = QueryGenerator.initQueryWrapper(algemeenDagblad, req.getParameterMap());
		Page<AlgemeenDagblad> page = new Page<AlgemeenDagblad>(pageNo, pageSize);
		IPage<AlgemeenDagblad> pageList = algemeenDagbladService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param algemeenDagblad
	 * @return
	 */
	@AutoLog(value = "每日汇报-添加")
	@ApiOperation(value="每日汇报-添加", notes="每日汇报-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody AlgemeenDagblad algemeenDagblad) {
		extracted(algemeenDagblad);
		updateWeekProgress(algemeenDagblad);
		algemeenDagbladService.save(algemeenDagblad);
		return Result.OK("添加成功！");
	}

	private void updateWeekProgress(AlgemeenDagblad algemeenDagblad) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
		LambdaQueryWrapper<WeeklyTasks> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(WeeklyTasks::getUser, user.getRealname());
		wrapper.eq(WeeklyTasks::getYear, year);
		wrapper.eq(WeeklyTasks::getYearWeek, weekOfYear);
		wrapper.ne(WeeklyTasks::getProgress, 100);
		WeeklyTasks one = weeklyTasksService.getOne(wrapper);
		one.setProgress(algemeenDagblad.getIndividualGoalYieldRate());
		weeklyTasksService.updateById(one);
	}

	private void extracted(AlgemeenDagblad algemeenDagblad) {
		LambdaQueryWrapper<SysUser> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(SysUser::getUsername, algemeenDagblad.getName());
		SysUser one = sysUserService.getOne(wrapper);
		MessageLogging messageLogging = new MessageLogging();
		messageLogging.setMessageType("每日汇报")
						.setCarbonCopyPerson(one.getRealname())
						.setCarbonCopyMassage(algemeenDagblad.getJobContent());
		messageLoggingService.save(messageLogging);
	}

	/**
	 *  编辑
	 *
	 * @param algemeenDagblad
	 * @return
	 */
	@AutoLog(value = "每日汇报-编辑")
	@ApiOperation(value="每日汇报-编辑", notes="每日汇报-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody AlgemeenDagblad algemeenDagblad) {
		algemeenDagbladService.updateById(algemeenDagblad);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "每日汇报-通过id删除")
	@ApiOperation(value="每日汇报-通过id删除", notes="每日汇报-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		algemeenDagbladService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "每日汇报-批量删除")
	@ApiOperation(value="每日汇报-批量删除", notes="每日汇报-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.algemeenDagbladService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "每日汇报-通过id查询")
	@ApiOperation(value="每日汇报-通过id查询", notes="每日汇报-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		AlgemeenDagblad algemeenDagblad = algemeenDagbladService.getById(id);
		if(algemeenDagblad==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(algemeenDagblad);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param algemeenDagblad
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AlgemeenDagblad algemeenDagblad) {
        return super.exportXls(request, algemeenDagblad, AlgemeenDagblad.class, "每日汇报");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AlgemeenDagblad.class);
    }

}
